<?php
// +----------------------------------------------------------------------
// | 萤火商城系统 [ 致力于通过产品和服务，帮助商家高效化开拓市场 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2023 https://www.yiovo.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed 这不是一个自由软件，不允许对程序代码以任何形式任何目的的再发行
// +----------------------------------------------------------------------
// | Author: 萤火科技 <admin@yiovo.com>
// +----------------------------------------------------------------------
declare (strict_types=1);

namespace app\api\controller;

use think\response\Json;
// use app\api\model\IndexBanner as IndexBannerModel;
// use app\api\model\IndexType as IndexTypeModel;
use app\console\service\UserGrade as UserGradeService;
use app\console\task\Order as Order;
use think\facade\Event;
use cores\exception\BaseException;
use app\store\model\User as UserModel;

/**
 * 根据积分自动升级会员等级
 * Class User
 * @package app\api
 */
class Points extends Controller
{

      /**
     * 更新当前用户的会员等级
     * * @param int $goodsId 商品ID
     * @param int|null $type 收藏类型 1 收藏 , 0 取消
     * @return Json
     * @throws \cores\exception\BaseException
     */
    public function setUserGrade()
    { 
        $php_token = $this->request->header('Access-Token');
        if($php_token != null && $php_token != "[object Boolean]"){
             $service = new UserGradeService;
             $service->setUserGradeNew($this->storeId);
             return $this->renderSuccess('更新成功');
        }else{
            return $this->renderSuccess('未获取到用户信息');
        }  
       
    }

    /**
     * 更新所有用户的会员等级
     * * @param int $goodsId 商品ID
     * @param int|null $type 收藏类型 1 收藏 , 0 取消
     * @return Json
     * @throws \cores\exception\BaseException
     */
    public function setUserGradeAll()
    { 
        $service = new UserGradeService;
        $service->setUserGrade($this->storeId);
        return $this->renderSuccess('更新成功'); 
       
    }


    /**
     * 执行定时事件
     * @return Json
     * @throws \cores\exception\BaseException
     */
    public function TaskHanle()
    { 
        Event::trigger('StoreTask');
        return $this->renderSuccess('执行完毕'); 
    }


    // public function TaskHanle1()
    // { 
    //     Event::trigger('Order', ['storeId' =>10001]);
    //     return $this->renderSuccess('执行完毕'); 
    // }


    /**
     * 会员等级进度条
     * @return Json
     * @throws \cores\exception\BaseException
     */
    public function schedule()
    { 
        $php_token = $this->request->header('Access-Token');
        if($php_token != null && $php_token != "[object Boolean]"){
             $service = new UserGradeService;
            $schedule = $service->getUserGradeDetail($this->storeId);
            return $this->renderSuccess(compact('schedule'));
        }else{
            $schedule = array();
            return $this->renderSuccess(compact('schedule'));
        } 
    }


   
}
